package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("api/item")
public class ItemApiController {


    @Autowired
    public ListFeignClient listFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    public ThreadPoolExecutor threadPoolExecutor;


    //数据汇总
    @GetMapping("/getItem/{skuId}")
    public Map<String, Object> getItem(@PathVariable Long skuId) {
        Map<String, Object> result = new HashMap<>();
        //查询5部分数据
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {

            //1:库存信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //2:三级分类
            BaseCategoryView baseCategoryView = productFeignClient.
                    getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //3:单查一个价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);


        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4:查询总销售属性 及属性值 及选中
            List<SpuSaleAttr> saleAttrAndValeAndChecked = productFeignClient.getSaleAttrAndValeAndChecked(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", saleAttrAndValeAndChecked);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5:所有销售属性组合与 skuId
            Map<String, String> skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));

        }, threadPoolExecutor);

        //增加热度
        CompletableFuture.runAsync(()->{
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);


        //等待所有的线程结束
        CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture
        ,priceCompletableFuture,spuSaleAttrListCompletableFuture,valuesSkuJsonCompletableFuture ).join();
        return result;
    }
}


